package leetcode_core.leetcode_2;

public class MyStack <V>{
    class ListNode <V>{
        V val;
        ListNode next;
        ListNode() {}
        ListNode(V val) { this.val = val; }
        ListNode(V val, ListNode next) { this.val = val; this.next = next; }
    }

    private ListNode<V> head;
    private int size;

    public MyStack() {
        head = null;
        size = 0;
    }

    public void offer(V val){
        if(head == null){
            head = new ListNode<>(val);
            this.size++;
            return;
        }else{
            ListNode<V> cur = new ListNode<>(val);
            cur.next = head;
            head = cur;
            this.size++;
        }

    }

    public V poll(){
        V val = null;
        if(head == null){
            return null;
        }else{
            this.size--;
            ListNode<V> pre = head.next;
            val = pre.val;
            head = pre;
        }
        return val;
    }

    public V peek(){
        V ans = null;
        if(head != null){
            ans = head.val;
        }
        return ans;
    }
}
